/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import model.Lid;
import model.Rit;
import org.hibernate.Session;
import org.hibernate.Transaction;
import persistence.SessionManager;

/**
 *
 * @author Johan
 */
public class RittenZoekenController extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        //hibernate session
        Session session = null;
        session = SessionManager.openSession();
        Transaction tx = session.beginTransaction();

        List<Rit> Ritten = new ArrayList<Rit>();

        String provincie = request.getParameter("provincie");
        String plaatsvan = request.getParameter("plaatsvan");
        String plaatsnaar = request.getParameter("plaatsnaar");
        String datum = request.getParameter("datum");
        int operator = Integer.parseInt(request.getParameter("radios"));

        System.out.println("provincie= " + provincie);
        System.out.println("plaatsvan= " + plaatsvan);
        System.out.println("plaatsnaar= " + plaatsnaar);
        System.out.println("operator= " + operator);

        String sqlquery = "SELECT rit_id FROM Rit WHERE rit_deleted != 1 AND rit_datum >= current_date ";
        if (provincie != "") {
            sqlquery += "AND rit_van_provincie = '" + provincie + "' ";
        }

        if (plaatsvan != "") {
            sqlquery += "AND rit_van_plaats = '" + plaatsvan + "' ";
        }

        if (plaatsnaar != "") {
            sqlquery += "AND rit_naar_plaats = '" + plaatsnaar + "' ";
        }

        if (datum != "") {
            String convertedDate = ConvertDate(datum);
            switch (operator) { //selectie radiobutton!!
                case 1:
                    sqlquery += "AND rit_datum = '" + convertedDate + "' ";
                    break;
                case 2:
                    sqlquery += "AND rit_datum > '" + convertedDate + "' ";
                    break;
                case 3:
                    sqlquery += "AND rit_datum < '" + convertedDate + "' ";
                    break;
            }

        }

        sqlquery += "ORDER BY rit_datum asc, rit_tijd asc";

        List<Integer> Ritnummers = session.createSQLQuery(sqlquery).list();

        if (Ritnummers.size() > 0) {
            for (int i = 0; i < Ritnummers.size(); i++) {
                Rit tempRit = (Rit) session.load(Rit.class, new Integer(Ritnummers.get(i)));
                Ritten.add(tempRit);
            }
        }

        request.setAttribute("ritten", Ritten);
        request.setAttribute("aantalRitten", Ritten.size());

        String address = "/ritten.jsp";
        RequestDispatcher dispatcher = request.getRequestDispatcher(address);
        dispatcher.forward(request, response);
    }

    private String ConvertDate(String date) {
        System.out.println("date = "+ date);
        String year = date.substring(6, 10);
        System.out.println("year = "+ year);
        String month = '-' + date.substring(3, 5) + '-';
        System.out.println("month = "+ month);
        String day = date.substring(0, 2);
        System.out.println("day = "+day);
        return year + month + day;
    }
}